<?php

class excel_export {
    
    
    
    public function export_unit_list() {
        
        $result = $this->exportUnitsToExcel();
        $file = $result['filepath'];
        header('Content-Disposition: attachment; filename="' . $result['filename'] . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Length: ' . filesize($file));
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        readfile($file);
    }
    
    public function exportUnitsToExcel() {  
        
        ini_set('display_errors', 1);
        error_reporting(E_ALL | E_STRICT);
        
        $libDir = dirname(dirname(__FILE__)) . '/excel';
        require_once $libDir . '/PHPExcel.php';
        require_once $libDir . '/PHPExcel/Writer/Excel2007.php';
        
        //Get php excel object
        $phpExcel = new PHPExcel();
        
        //Set column active
        $phpExcel->setActiveSheetIndex(0);
        $activeSheet = $phpExcel->getActiveSheet();
        $activeSheet->setTitle();
        
        //Set width of column
        $phpExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
        $phpExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $phpExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $phpExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
        $phpExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
                
        $activeSheet->setCellValue('A1', 'kaka' );
        
        //Set date time of file
        $my_t=getdate(date("U"));
        $dateTime = "$my_t[weekday] $my_t[mday], $my_t[month]  $my_t[year]";
        $activeSheet->setCellValue('D1', $dateTime);
        
        //Set border
        $phpExcel->getActiveSheet()->getStyle('A1:E1')->applyFromArray(
            array(
                'borders' => array(
                    'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
                ),
                'color' => PHPExcel_Style_Color::COLOR_BLUE
            )
        );
        
        $phpExcel->getActiveSheet()->getStyle('E18')->applyFromArray(
            array(
                'borders' => array(
                    'left'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
                    'right'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
                )
            )
        );
        
        //Set title bold
        $activeSheet->getStyle('A1')->getFont()->setBold(true);
        $activeSheet->getStyle('D1')->getFont()->setBold(true);
        $activeSheet->getStyle('A7')->getFont()->setBold(true);
        $activeSheet->getStyle('D7')->getFont()->setBold(true);
        $activeSheet->getStyle('A12')->getFont()->setBold(true);
        $activeSheet->getStyle('B12')->getFont()->setBold(true);
        $activeSheet->getStyle('B7')->getFont()->setBold(true);
        
        $activeSheet->getStyle('A18')->getFont()->setBold(true);
        $activeSheet->getStyle('B18')->getFont()->setBold(true);
        $activeSheet->getStyle('C18')->getFont()->setBold(true);
        $activeSheet->getStyle('D18')->getFont()->setBold(true);
        $activeSheet->getStyle('E18')->getFont()->setBold(true);
        
        //Not group
        $activeSheet->setCellValue('A18', 'User name');
        $activeSheet->setCellValue('B18', 'Description');
        $activeSheet->setCellValue('C18', 'Date');
        $activeSheet->setCellValue('D18', 'Project');
        $activeSheet->setCellValue('E18', 'Hour');
      
        
        //Get data
        $rowNum = 19;
        $totalRecord = 10;
        
        for ( $i = 0; $i < $totalRecord; $i++ ) {  
            $activeSheet->setCellValue('A' . $rowNum, 'abc');
            $activeSheet->setCellValue('B' . $rowNum, 'abc');
            $activeSheet->setCellValue('C' . $rowNum, 'abc');
            $activeSheet->setCellValue('D' . $rowNum, 'abc');
            $activeSheet->setCellValue('E' . $rowNum, 'abc');
            $rowNum++;
        }
        
        //Set border of table
        $phpExcel->getActiveSheet()->getStyle('A18:E28')->applyFromArray(
            array(
                'borders' => array(
                    'left'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
                    'right'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
                    'top'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
                    'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN)
                )
            )
        );
        
        $phpExcel->getActiveSheet()->getStyle('A18:E18')->applyFromArray(
            array(
                'borders' => array(
                    'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
                    'top'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
                )
            )
        );
        
        //Set last column of table
        $rowBorder = $rowNum + 2;
        $phpExcel->getActiveSheet()->getStyle('E18:E'.$rowBorder.'')->applyFromArray(
            array(
                'borders' => array(
                    'left'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
                    'right'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
                    'top'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
                    'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
                )
            )
        );
        
       $rowBorder = $rowNum - 1;
        $phpExcel->getActiveSheet()->getStyle('A'.$rowBorder.':E'.$rowBorder)->applyFromArray(
            array(
                'borders' => array(
                    'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
                ),
                
            )
        );
        
        //Set border of result
        $rowBorderStart = $rowNum;
        $rowBorderStop  = $rowNum + 2;
        $phpExcel->getActiveSheet()->getStyle('D'.$rowBorderStart.':E'.$rowBorderStop)->applyFromArray(
            array(
                'borders' => array(
                    'left'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
                    'right'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
                    'top'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
                    'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
                )
            )
        );
        
        //Set underline
        $styleArray = array( 'font' => array( 'bold' => true, 'underline' => PHPExcel_Style_Font::UNDERLINE_SINGLE),);
        $phpExcel->getActiveSheet()->getStyle('A7')->applyFromArray( $styleArray );
        $phpExcel->getActiveSheet()->getStyle('A12')->applyFromArray( $styleArray );
        
        $objWriter = new PHPExcel_Writer_Excel2007($phpExcel);
        $filename = 'avf.xlsx';
        $filepath = sys_get_temp_dir() . '/' . $filename;
        $objWriter->save($filepath);
        return array('filename' => $filename, 'filepath' => $filepath);
    }
}

$excel = new excel_export();
$excel->export_unit_list();